package com.msg.xml;

import java.util.ArrayList;

/*
 * find the event name from the xml file,
 * and write it to database if such event doesn't exist in the database
 * @author jeson9603
 *
 */
public class Eve_Opp_Manipulator extends Table_Manipulator {
	private String eventCode;
	private String opponentName;
	private String eventDate;
	private String season;
	
	public Eve_Opp_Manipulator(Property prop) {
		super(prop);
	}
	
	/**
	 * read the event code from the XML file
	 * @return : the string of event code
	 */
	private String getEventCode() {
		if(eventCode == null)
			eventCode = XMLhandler.getDataByTag("EVENT:");
		return eventCode;
	}
	
	/**
	 * read the event date from XML file
	 * @return : the string of event date
	 */
	private String getEventDate() {
		if(eventDate == null)
			eventDate = XMLhandler.getDataByTag("EVENT DATE / TIME:");
		return eventDate;
	}
	
	/**
	 * read the season for an event from XML file
	 * @return
	 */
	private String getEventSeason() {
		if(season == null) {
			int startIndex = eventDate.indexOf(',')+1;
			season = (String)eventDate.subSequence(startIndex, startIndex+4);
		}
		return season;
	}
	
	/**
	 * get the name of opponent from XML file
	 * @return
	 */
	private String getOpponent() {
		if(opponentName == null)
			opponentName = XMLhandler.getDataByTag("TEAM:");
		return opponentName;
	}
	
	/**
	 * get the event code, event data, event session and opponent 
	 */
	@Override
	protected String getSpecificData() {
		getEventCode();
		getEventDate();
		getEventSeason();
		getOpponent();
		StringBuilder builder = new StringBuilder();
		builder.append(eventCode + ",");
		builder.append(eventDate + ",");
		builder.append(season + ",");
		builder.append(opponentName);
		return builder.toString();
	}
	
	/**
	 * update the opponents table and event table
	 */
	@Override
	protected String update() {
		ArrayList<String> id = updateOpponents();
		return updateEvent(id);
	}
	
	@Override
	protected void pushData(Object o) {
	}
	
	/**
	 * update the Opponents table
	 * check if the opponent record has already in the table, if is not, add it into Opponents table
	 * @return : the opponent_Id of given Opponent_Name
	 */
	private ArrayList<String> updateOpponents() {
		DBhandler = DBHandler.getInstance();
		
		//create a request wrapper
		DBRequest request = new DBRequest();
		request.resultCol.add("Opponents_Id");
		request.entries.add("Opponents_Name");
		request.data.add(opponentName);
		
		ArrayList<String> opponentsId = DBhandler.getData("Opponents", request);
		
		//no opponent record in database, add this opponent record into database
		if(opponentsId.size() == 0)
			DBhandler.insertData("Opponents", request);
		
		opponentsId = DBhandler.getData("Opponents", request);
		return opponentsId;
	}
	
	/**
	 * update the event table
	 * check if the event record has already exist, if is not, add it into event table
	 * @param opponentId : the value of opponentId for adding a new event
	 */
	private String updateEvent(ArrayList<String> opponentIds) {
		DBhandler = DBHandler.getInstance();
		
		DBRequest request;
		for(int i = 0; i < opponentIds.size(); i++) {
			request = new DBRequest();
			request.resultCol.add("Event_Id");
			request.entries.add("Opponents_Id");
			request.entries.add("Event_Code");
			request.entries.add("Event_Date");
			request.entries.add("Season");
			
			String opponentId = opponentIds.get(i);
			//opponentId = opponentId.substring(0, opponentId.indexOf(','));
			request.data.add(opponentId);
			request.data.add(eventCode);
			request.data.add(formatDate(eventDate));
			request.data.add(season);
			
			if(!DBhandler.hasData("event", request)) {
				DBhandler.insertData("event", request);
				prop.setInitialEvent(true);
			}
			prop.setEVENT_ID(DBhandler.getData("event", request).get(0));
		}
		return prop.getEVENT_ID();
	}
}
